﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;

public class Ban4Throwable : Throwable
{
    //爆炸特效预物体.
    public GameObject explosionEffect;

    //当物体被扔出去的瞬间，执行此函数.
    protected override void OnDetachedFromHand(Hand hand)
    {
        //调用基类的同名函数.
        base.OnDetachedFromHand(hand);

        Debug.LogWarning("我被扔出去了！！！！");
        //3秒后，爆炸.
        Invoke("Explored", 3f);
    }

    //爆炸函数.
    void Explored()
    {
        Instantiate(explosionEffect, transform.position, Quaternion.identity);
        //判断爆炸效果周围有没有敌人.
        int layerMask = LayerMask.NameToLayer("Enemy");
        layerMask = 1 << layerMask;
        Collider[] colArray = Physics.OverlapSphere(transform.position, 3, layerMask);
        if(colArray!=null && colArray.Length>0)
        {
            for(int i=0;i< colArray.Length;i++)
            {
                //获得敌人身上的组件Ban4Enemy的实例，调用其公有函数OnHurt().
                Ban4Enemy enemy = colArray[i].GetComponent<Ban4Enemy>();
                if(enemy!=null)
                {
                    enemy.OnHurt();
                }
            }
        }
    }
}
